﻿//#define CSHARP_5 // for using Here with .NET prior to 4.5, comment this line

/*
The MIT License (MIT)
Copyright (c) 2013 kaveh.shahbazian@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
IN THE SOFTWARE.
*/

using System;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace Buddy
{
#if CSHARP_5
    public static class Here
    {
        public static string Spot([CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0, [CallerMemberName] string memberName = "")
        {
            return string.Format("@{0} #{1} {2}", Path.GetFileName(sourceFilePath), sourceLineNumber, memberName);
        }

        public static string Member([CallerMemberName] string memberName = "")
        {
            return memberName;
        }

        public static string FileLine([CallerLineNumber] int sourceLineNumber = 0)
        {
            return sourceLineNumber.ToString();
        }

        public static string FileName([CallerFilePath] string sourceFilePath = "")
        {
            return Path.GetFileName(sourceFilePath);
        }
    }
#else
    public static class Here
    {
        public static string Spot(HereOptions options)
        {
            return MakeMessageInternals(options, 3);
        }

        public static string Type
        {
            get
            {
                return MakeMessageInternals(HereOptions.Type, 3);
            }
        }

        public static string Member
        {
            get
            {
                return MakeMessageInternals(HereOptions.Member, 3);
            }
        }

        public static string FileLine
        {
            get
            {
                return MakeMessageInternals(HereOptions.FileLine, 3);
            }
        }

        public static string FileName
        {
            get
            {
                return MakeMessageInternals(HereOptions.FileName, 3);
            }
        }

        static StackFrame GetCaller(int index) { return new StackTrace(true).GetFrame(index); }

        static string MakeMessageInternals(HereOptions options, int index)
        {
            StackFrame first = null;
            var _FileName = new StringBuilder();
            var _FileLine = new StringBuilder();
            var _Member = new StringBuilder();
            var _Type = new StringBuilder();

            if ((options & HereOptions.FileName) == HereOptions.FileName)
            {
                first = GetCaller(index);
                if (first != null)
                {
                    var fn = first.GetFileName();
                    if (!string.IsNullOrEmpty(fn))
                        _FileName.Append(" " + Path.GetFileName(fn));
                }
            }

            if ((options & HereOptions.FileLine) == HereOptions.FileLine)
            {
                if (first == null)
                    first = GetCaller(index);
                if (first != null)
                {
                    var ln = first.GetFileLineNumber();
                    _FileLine.Append(" #" + ln);
                }
            }

            if ((options & HereOptions.Member) == HereOptions.Member)
            {
                if (first == null)
                    first = GetCaller(index);
                if (first != null)
                {
                    var mn = first.GetMethod().Name;
                    _Member.Append(" " + mn);
                }
            }

            if ((options & HereOptions.Type) == HereOptions.Type)
            {
                if (first == null)
                    first = GetCaller(index);
                if (first != null)
                {
                    var dt = first.GetMethod().DeclaringType;
                    _Type.Append(string.Format(" ({0})", dt));
                }
            }

            var messageInternal = string.Format("{0}{1}{2}{3}",
                _FileName,
                _FileLine,
                _Member,
                _Type).Trim();

            return messageInternal;
        }
    }

    [Flags]
    public enum HereOptions : byte
    {
        None = 0,
        FileName = 1,
        FileLine = 2,
        Member = 4,
        Type = 8,
        All = 15
    }
#endif
}
